package com.dangdi.show.controller;

import com.dangdi.show.service.ManagerService;
import com.dangdi.show.service.WechatUserService;
import com.dangdi.show.util.Xml_MapUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

@RestController
public class WechatPublicController {
    @Autowired
    WechatUserService userService;
    @Resource
    ManagerService managerService;

    @RequestMapping("getBasePropertis")
    public Map<String, Object> getBasePropertis() {
        Map<String, Object> propety = new HashMap<>();
        List<Map<String, Object>> indexSideUrls = userService.getIndex_side_imgS();
        List<String> tableImgUrls = new ArrayList<String>();
        propety.put("sideUrls", indexSideUrls);
        tableImgUrls.add("");
        propety.put("publishList", managerService.getScenehList(0, 99));
        propety.put("indexTitleImg", managerService.getIndexHaiBaoTu());
        propety.put("indexActivity", managerService.getIndexHuodong());
        return propety;
    }

    @RequestMapping("huidiao")
    public void huidiao(HttpServletRequest request) throws IOException, DocumentException {
        System.out.println("进来了");
        String body = getParm(request);
        Map<String, Object> xmlBody = Xml_MapUtils.xmlToMap(body);
        userService.updateTransaction(xmlBody.get("transaction_id").toString(), xmlBody.get("out_trade_no").toString());
    }

    /**
     * 获取POST请求中Body参数
     *
     * @param request
     * @return 字符串
     */
    public String getParm(HttpServletRequest request) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String line = null;
        StringBuilder sb = new StringBuilder();
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sb.toString();
    }

}
